package cn.edu.lingnan.servlet;

import cn.edu.lingnan.pojo.Student;
import cn.edu.lingnan.service.IStudentService;
import com.mysql.cj.Session;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

@WebServlet("/queryAllStu")
public class QueryStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1
        String stuName = req.getParameter("stuName");
        //2
        IStudentService service = new IStudentService();
        List<Student> list = service.findStudentAll();
        List<Student> studentByNameList = service.getStudentByName(stuName);
        //System.out.println(list.toString());
        HttpSession session = req.getSession();
        if(stuName==null||stuName.equals("")) {
            session.setAttribute("allStu",list);//默认查询全部学生
        }else {
            session.setAttribute("allStu", studentByNameList);//进行模糊查询
        }

        //3
        resp.sendRedirect(req.getContextPath()+"admin/student.jsp");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
